package com.atguigu.union;

import com.atguigu.function.WaterSensorFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author yhm
 * @create 2024-04-03 9:23
 */
public class Test01_union {
    public static void main(String[] args) throws Exception {
        // 1. 创建环境
        Configuration conf = new Configuration();
        conf.setInteger("rest.port",8081);

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        // 2. 读取数据源
        DataStreamSource<String> dataStreamSource1 = env.socketTextStream("hadoop102", 7777);
        DataStreamSource<String> dataStreamSource2 = env.socketTextStream("hadoop102", 8888);


        // 3. 处理数据
        DataStream<String> dataStream = dataStreamSource1.union(dataStreamSource2);

        // 先将数据转换为javaBean 按照id进行分组  聚合vc的值
        dataStream.flatMap(new WaterSensorFunction())
                .keyBy(sensor -> sensor.id)
                .sum("vc").print();

        // 4. 输出

        // 5. 执行环境
        env.execute();
    }
}
